package org.example.infrastructure.repository.orm.database;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.domain.model.entity.url.UrlShortenerAccessLogs;
import org.example.domain.model.entity.url.req.UrlShortenerGroupStatsAccessRecordReq;
import org.example.domain.model.entity.url.req.UrlShortenerGroupStatsReq;
import org.example.domain.model.entity.url.req.UrlShortenerStatsAccessRecordReq;
import org.example.domain.model.entity.url.req.UrlShortenerStatsReq;
import org.example.domain.model.entity.url.vo.UrlShortenerAccessLogsVo;
import org.example.domain.repository.UrlShortenerAccessLogsRepository;
import org.example.infrastructure.persistence.orm.UrlShortenerAccessLogsMapper;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
@RequiredArgsConstructor
public class MybatisUrlShortenerAccessLogsRepository implements UrlShortenerAccessLogsRepository {
    private final UrlShortenerAccessLogsMapper urlShortenerAccessLogsMapper;

    @Override
    public void insertUrlShortenerAccessLogs(UrlShortenerAccessLogs urlShortenerAccessLogs) {
        urlShortenerAccessLogsMapper.insertUrlShortenerAccessLogs(urlShortenerAccessLogs);
    }

    @Override
    public UrlShortenerAccessLogsVo  findPvUvUidStatsByUrlShortener(UrlShortenerStatsReq urlShortenerStatsReq) {
        return urlShortenerAccessLogsMapper.findPvUvUidStatsByUrlShortener(urlShortenerStatsReq);
    }

    @Override
    public List<HashMap<String, Object>> topIpByUrlShortenerList(UrlShortenerStatsReq urlShortenerStatsReq) {
        return urlShortenerAccessLogsMapper.topIpByUrlShortenerList(urlShortenerStatsReq);
    }

    @Override
    public HashMap<String, Object> findUvTypeByUrlShortenerList(UrlShortenerStatsReq urlShortenerStatsReq) {
        return urlShortenerAccessLogsMapper.findUvTypeByUrlShortenerList(urlShortenerStatsReq);
    }

    @Override
    public PageInfo<UrlShortenerAccessLogs> selectAccessLogsGroupPage(UrlShortenerGroupStatsAccessRecordReq req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        List<UrlShortenerAccessLogs> urlShortenerAccessLogs = urlShortenerAccessLogsMapper.selectAccessLogsGroupPage(req);
        return new PageInfo(urlShortenerAccessLogs);
    }

    @Override
    public List<Map<String, Object>> selectGroupUvTypeByUsers(String gid, String startDate, String endDate, List<String> userAccessLogsList) {
        return urlShortenerAccessLogsMapper.selectGroupUvTypeByUsers(gid, startDate, endDate, userAccessLogsList);
    }

    @Override
    public PageInfo<UrlShortenerAccessLogs> selectStatsAccessRecordPage(UrlShortenerStatsAccessRecordReq req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        List<UrlShortenerAccessLogs> urlShortenerAccessLogs = urlShortenerAccessLogsMapper.selectStatsAccessRecordPage(req.getFullShortUrl(), req.getStartDate(), req.getEndDate());
        return new PageInfo(urlShortenerAccessLogs);
    }

    @Override
    public List<Map<String, Object>> selectUvTypeByUsers(String gid, String fullShortUrl, Integer enableStatus, String startDate, String endDate, List<String> userAccessLogsList) {
        return urlShortenerAccessLogsMapper.selectUvTypeByUsers(gid, fullShortUrl, enableStatus, startDate, endDate,userAccessLogsList);
    }

    @Override
    public UrlShortenerAccessLogsVo findUrlShortenerAccessLogsVoByGroup(UrlShortenerGroupStatsReq urlShortenerGroupStatsReq) {
        return urlShortenerAccessLogsMapper.findUrlShortenerAccessLogsVoByGroup(urlShortenerGroupStatsReq);
    }

    @Override
    public List<HashMap<String, Object>> topIpByGroupList(UrlShortenerGroupStatsReq urlShortenerGroupStatsReq) {
        return urlShortenerAccessLogsMapper.topIpByGroupList(urlShortenerGroupStatsReq);
    }
}
